﻿using qcloudsms_csharp;
using qcloudsms_csharp.httpclient;
using qcloudsms_csharp.json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WaiShop.Remote
{
    /// <summary>
    /// 
    /// </summary>
    public class TencentSMS
    {
        //appId
        public int appId;
        //appKey
        public string appKey = "";
        //短信模板ID
        private int tmplateId = 379257;
        //private int tmplateId =382682;
        //签名内容
        private string smsSign = "7hhhcn";
        //private string smsSign = "bdqnt1818203";


        /// <summary>
        /// 验证码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 创建对象时随机产生验证码
        /// </summary>
        public TencentSMS()
        {
            Random random = new Random();
            Code = random.Next(100000, 999999);
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public bool SetSMS(string phone)
        {
            bool flag = false;
            try
            {
                SmsSingleSender ssender = new SmsSingleSender(appId, appKey);
                var result = ssender.sendWithParam("86", phone,
                    tmplateId, new[] { Code.ToString() }, smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
                flag = true;
            }
            catch (JSONException ex)
            {

            }
            catch (HTTPException ex)
            {

            }
            catch (Exception ex)
            {

            }
            return flag;
        }
    }
}
